package soccerscope.training;

import java.util.LinkedList;

/**
 * This class represents the statistics in trainingszenarios. In general it
 * manages a LinkedList of TrainingStatsElement.
 */
public class TrainingStatistics {

  private LinkedList<TrainingStatsElement> elements;

  // constructor
  public TrainingStatistics() {

    super();
    this.elements = new LinkedList();
    // counter for training runs is always added
    this.addElement(0);
  }

  // adds a new Counter to the statistics
  public void addElement(int index) {

    // only add the element, if it isn't already in use
    if (this.indexInList(index) == -1) {
      this.elements.add(new TrainingStatsElement(index));
    }
  }

  // increases the counter with the index n
  public void incElement(int index) {

    int place = this.indexInList(index);
    // first we need to check if we have an element with such an index
    if (!(place == -1)) {
      this.elements.get(place).inc();
    }
  }

  // returns a training-counter
  public TrainingStatsElement getElement(int place) {

    if ((place > -1) && (place < this.elements.size())) {
      return this.elements.get(place);
    }
    System.err.println("[TrainingStatistics] Try to get not existant element");
    return null;
  }

  // returns the array of all training stats
  public TrainingStatsElement[] getElementsArray() {

    int s = this.elements.size();
    TrainingStatsElement[] els = new TrainingStatsElement[s];
    for (int i = 0; i < s; i++) {
      els[i] = (TrainingStatsElement) (this.elements.get(i));
    }
    return els;
  }

  // _____________________________________________________________
  // HELP METHODS
  // _____________________________________________________________

  private int indexInList(int index) {

    for (int i = 0; i < this.elements.size(); i++) {
      if (this.elements.get(i).getIndex() == index) {
        return i;
      }
    }
    return -1;
  }
}